//
//  BGRecordViewController.swift
//  Billing
//
//  Created by Dale on 17/4/19.
//  Copyright © 2017年 Nari. All rights reserved.
//

import UIKit

class BGRecordViewController: BCFatherViewController, UITableViewDelegate, UITableViewDataSource  {
    
    @IBOutlet weak var tableView: UITableView!
    var dataSource = [Rocord]()
    var page: Int = 0
    var isLoad: Bool?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "账单"
        tableView.emptyDataSetSource = self
        tableView.emptyDataSetDelegate = self
        
        Common.removeExtraCellLines(TableView: tableView)
        isRefrsh = true
        //MARK: - 下拉刷新
        refreshData()
    }
    //MARK: - 加载数据
    func loadData() -> Void {
        if  isRefrsh {
            SVProgressHUD.show()
        }
        BmobManager.queryRecord(page: self.page) { (reslut, status) -> (Void) in
            if status == .NRSSuccess {
                self.dataSource.removeAll()
                print("page: \(self.page)")
                SVProgressHUD.dismiss()
                let tmpArr = reslut as! Array<Any>
                for temp in tmpArr {
                    self.dataSource.append(temp as! Rocord)
                }
                self.tableView.reloadData()
                if self.dataSource.count <= 0 {
                    self.tableView.reloadEmptyDataSet()
                }
                if (self.dataSource.count >= 10) {
                    //MARK: - 上拉加载更多数据
                    self.loadMoreData()
                } else {
                    self.tableView.mj_footer = nil
                }
            } else {
                SVProgressHUD.showError(withStatus: reslut as! String)
            }
        }
    }
    
    //MARK: - 上拉加载更多
    func loadMoreData() -> Void {
        self.tableView.mj_footer = MJRefreshBackNormalFooter(refreshingBlock: {
            self.page = self.page + 1
            BmobManager.queryRecord(page: self.page) { (reslut, status) -> (Void) in
                if status == .NRSSuccess {
                    print("page: \(self.page)")
                    let tmpArr = reslut as! Array<Any>
                    if(tmpArr.count < 10) {
                        //最后一页
                        self.tableView.mj_footer.endRefreshingWithNoMoreData()
                    }
                    else {
                        self.tableView.mj_footer.endRefreshing()
                    }
                    for temp in tmpArr {
                        self.dataSource.append(temp as! Rocord)
                    }
                    self.tableView.reloadData()
                } else {
                    self.tableView.mj_footer.endRefreshing()
                    SVProgressHUD.showError(withStatus: reslut as! String)
                }
            }
        })
    }
    
    //MARK: - 下拉刷新
    func refreshData() -> Void {
        self.tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            self.page = 0
            BmobManager.queryRecord(page: self.page) { (reslut, status) -> (Void) in
                if status == .NRSSuccess {
                    print("page: \(self.page)")
                    SVProgressHUD.dismiss(withDelay: 1.0, completion: {
                        if self.dataSource.count > 0 {
                            self.dataSource.removeAll()
                        }
                        let tmpArr = reslut as! Array<Any>
                        for temp in tmpArr {
                            self.dataSource.append(temp as! Rocord)
                        }
                        self.tableView.mj_header.endRefreshing()
                        self.tableView.reloadData()
                        if self.dataSource.count <= 0 {
                            self.tableView.reloadEmptyDataSet()
                        }
                        if (self.dataSource.count >= 10) {
                            //MARK: - 上拉加载更多数据
                            self.loadMoreData()
                        } else {
                            self.tableView.mj_footer = nil
                        }
                    })
                } else {
                    self.tableView.mj_header.endRefreshing()
                    SVProgressHUD.showError(withStatus: reslut as! String)
                }
            }
        })
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if isRefrsh {
            self.page = 0
            loadData()
            isRefrsh = false
        }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = BGRecordCell.cellWithTableView(tableView: tableView)
        cell.configureCell(model: self.dataSource[indexPath.row])
        cell.modifyClosure = { () in
            let bgRecordDetailVC = BGRecordDetailViewController()
            bgRecordDetailVC.hidesBottomBarWhenPushed = true
            bgRecordDetailVC.record = self.dataSource[indexPath.row]
            self.navigationController?.pushViewController(bgRecordDetailVC, animated: true)
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 70
    }
    
}

extension BGRecordViewController:DZNEmptyDataSetSource {
    
    func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
        var text:String? = nil
        var font:UIFont? = nil
        var textColor:UIColor? = nil
        let attributes:NSMutableDictionary = NSMutableDictionary()
        
        text = "我还没吃饭呢~"
        font = UIFont.boldSystemFont(ofSize: 17.0);
        textColor = UIColor(hex: "545454")
        
        if (text == nil) {
            return nil
        }
        
        if (font != nil) {
            attributes.setObject(font!, forKey: NSFontAttributeName as NSCopying)
        }
        if (textColor != nil) {
            attributes.setObject(textColor!, forKey: NSForegroundColorAttributeName as NSCopying)
        }
        return NSAttributedString(string: text!, attributes: attributes as? [String : Any])
    }
    
    func description(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
        var text:String? = nil
        var font:UIFont? = nil
        var textColor:UIColor? = nil
        let attributes:NSMutableDictionary = NSMutableDictionary()
        
        let paragraph = NSMutableParagraphStyle()
        paragraph.lineBreakMode = .byWordWrapping
        paragraph.alignment = .center
        
        text = "这里将展示您所有的吃饭记录"
        font = UIFont.boldSystemFont(ofSize: 15)
        textColor = UIColor(hex: "545454")
        if (font != nil) {
            attributes.setObject(font!, forKey: NSFontAttributeName as NSCopying)
        }
        if (textColor != nil) {
            attributes.setObject(textColor!, forKey: NSForegroundColorAttributeName as NSCopying)
        }
        attributes.setObject(paragraph, forKey: NSParagraphStyleAttributeName as NSCopying)
        
        return NSAttributedString(string: text!, attributes: attributes as? [String : Any])
        
    }
    
    func verticalOffset(forEmptyDataSet scrollView: UIScrollView) -> CGFloat {
        return -64.0
    }
    
    func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
        return .white
    }
    
    func spaceHeight(forEmptyDataSet scrollView: UIScrollView) -> CGFloat {
        return 10.0
    }
    
}

extension BGRecordViewController:DZNEmptyDataSetDelegate {
    
    func emptyDataSetShouldDisplay(_ scrollView: UIScrollView) -> Bool {
        return true
    }
    
    func emptyDataSetShouldAllowTouch(_ scrollView: UIScrollView) -> Bool {
        return true
    }
    
    func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
        return true
    }
    
    func emptyDataSetShouldAnimateImageView(_ scrollView: UIScrollView) -> Bool {
        return true
    }
    
    func emptyDataSet(_ scrollView: UIScrollView, didTap view: UIView) {
        print("Tap View")
    }
    
    func emptyDataSet(_ scrollView: UIScrollView, didTap button: UIButton) {
        print("Tap Button")
    }
    
}

